<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Admin\CatesController;
use App\Models\Goods;
use App\Http\Controllers\Controller;
use DB;

class ListController extends Controller
{
    public function getIndex(Request $request)
    {
        $condition = 'created_at';
        $second = 'desc';

        $cateid = '';
        $cateUrl = '';
        if($request->cateid){
            $cateid = $request->cateid;
            $cateUrl = '&cateid='.$request->cateid;
        }

        if($request->condition){
            switch ($request->condition){
                case 'hot':
                    $condition = 'sale_num';
                    $second = 'desc';
                    break;
                case 'new':
                    $condition = 'created_at';
                    $second = 'desc';
                    break;
                case 'top':
                    $condition = 'goods_price';
                    $second = 'desc';
                    break;
                case 'bot':
                    $condition = 'goods_price';
                    $second = '';
                    break;
            }
        }

        $cates = CatesController::getCatesByPid(0);
        $count = Goods::count();

        if($second){
            if($cateid){
                $goods = Goods::where('cate_id', $cateid)->orderBy($condition, $second)->paginate(12);
            }else{
                $goods = Goods::orderBy($condition, $second)->paginate(12);
            }
        }else{
            if($cateid){
                $goods = Goods::where('cate_id', $cateid)->orderBy($condition)->paginate(12);
            }else{
                $goods = Goods::orderBy($condition)->paginate(12);
            }
        }

        $new = DB::table('goods')->skip(0)->take(6)->orderBy('created_at','desc')->get();
        $sale = DB::table('goods')->skip(0)->take(3)->orderBy('sale_num','desc')->get();
        $hot = DB::table('goods')->skip(0)->take(3)->orderBy('visit_num','desc')->get();
        $best = DB::table('goods')->skip(0)->take(4)->where('is_best',1)->orderBy('created_at','desc')->get();

        return view('Home/List/index', [
            'cates'=>$cates,
            'cateUrl'=>$cateUrl,
            'goods'=>$goods,
            'count'=>$count,
            'request'=>$request,
            'hot'=>$hot,
            'new'=>$new,
            'sale'=>$sale,
            'best'=>$best
        ]);
    }

}
